<?php

/**
 *
 * @author Administrator
 */
class TourController {
    // TODO - Insert your code here
    private $path;
    private $lan;
    
    /**
     */
    function __construct() {
        
        $this->path = "../src/module/admin/view_tours/temp/";
        $this->hotArray = array(1 => "Tour du lịch nổi bật", 0 => "Không phải tour bật");
        $this->feshArray = array(1 => "Tour du lịch mới", 0 => "Không phải tour mới");
        

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }
    
    /**
     * view table page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewTable(){
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/table.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_TOURS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
        
        $template->assign( TYPE, $_REQUEST[PARAM_TYPE]);
        
        
        // get array cate product
        $carCtrl = new CategoryController( "");
        $cateArray = $carCtrl->adminCateArray($_REQUEST[PARAM_TYPE]);
        $cateArray['no'] = "--- Không có danh mục ----";
        
        // select list
        $db = new DBAccess();
        if (isset($_REQUEST['CategoryID'])){
            $selected = $_REQUEST['CategoryID'];
            if ($selected == ""){
                $list = $db->selectManyByCauseWithPaging( $GLOBALS['tourFullColArray'], TOURS, array( TOUR_TYPE => $_REQUEST[PARAM_TYPE], SQL_AND, TOUR_LANGUAGE => $this->lan), array(TOUR_ID => DESC), 1, 10000);
            } else if ($selected == "no"){
                $conArrayVaule = array();
                $conArrayVaule = createSubIDArray(0, $conArrayVaule, $_REQUEST[PARAM_TYPE]);
                $conArray = array( array('key' => LIKE, 'column' => TOUR_TYPE, 'value' => $_REQUEST[PARAM_TYPE]), SQL_AND, array('key' => NOT_IN, 'column' => TOUR_CATEGORYID, 'value' => $conArrayVaule), SQL_AND, array('key' => LIKE, 'column' => TOUR_LANGUAGE, 'value' => $this->lan));
                $list = $db->selectDifficultRecord( $GLOBALS['tourFullColArray'], TOURS, $conArray, array(TOUR_ID => DESC), 1, 10000);
            } else {
                $list = $db->selectDifficultRecord( $GLOBALS['tourFullColArray'], TOURS, createSubConditionByID($selected, TOUR_CATEGORYID, $this->lan), array(TOUR_ID => DESC), 1, 10000);
            }
        } else {
            $selected = "";
            $list = $db->selectManyByCauseWithPaging( $GLOBALS['tourFullColArray'], TOURS, array( TOUR_TYPE => $_REQUEST[PARAM_TYPE], SQL_AND, TOUR_LANGUAGE => $this->lan), array(TOUR_ID => DESC), 1, 10000);
        }
        
        // assign item
        foreach ( $list as $item ) {
            $cate = $db->selectOneByCause(array(CATEGORY_TITLE), CATEGORIES, array(CATEGORY_ID => $item[TOUR_CATEGORYID]));
            $item[TOUR_CATEGORYID] = $cate[CATEGORY_TITLE];
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        $selectCate = createSelectTagFormData( TOUR_CATEGORYID, "abc", $cateArray, $selected, "this.form.submit()");
        $template->assign( 'SelectCate', $selectCate);
        
        // assign total count and total page
        $count = $db->selectCountRecord( TOURS, array(TOUR_LANGUAGE => $this->lan));
        $template->assign( TOTALCOUNT, $count);
        $template->assign( TOTALPAGE, 1);
        
        // assign table title
        $template->assign( TABLETITLE, $GLOBALS['tableTitleArray'][TOURS][$_REQUEST[PARAM_TYPE]]);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }
    
    /**
     * view form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm(){
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_TOURS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        // get id
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            $db = new DBAccess();
            $item = $db->selectOneByCause( $GLOBALS['tourFullColArray'], TOURS, array(TOUR_ID => $_REQUEST[PARAM_ID]));
            $title = $GLOBALS['formTitleArray'][TOURS][EDIT][$this->lan] . $item[TOUR_TITLE];
        } else {
            $item = array();
			$item[TOUR_ID] = 0;
			$item[TOUR_TYPE] = $_REQUEST[PARAM_TYPE];
			$item[TOUR_CATEGORYID] = 0;
			$item[TOUR_PRICE] = 0;
			$item[TOUR_CURRENCY] = "VND";
			$item[TOUR_SHOWPRICE] = 0;
			$item[TOUR_SHOWCURRENCY] = "VND";
			$item[TOUR_PROMOTION] = "";
			$item[TOUR_HOT] = 0;
			$item[TOUR_FESH] = 0;
            $title = $GLOBALS['formTitleArray'][TOURS][ADD][$this->lan];
        }
        
        $item[TOUR_PRICE] = addComma( $item[TOUR_PRICE]);
        $item[TOUR_SHOWPRICE] = addComma( $item[TOUR_SHOWPRICE]);
        $item[TOUR_HOT] = createSelectTagFormData( TOUR_HOT, "full-width", $this->hotArray, $item[TOUR_HOT]);
        $item[TOUR_FESH] = createSelectTagFormData( TOUR_FESH, "full-width", $this->feshArray, $item[TOUR_FESH]);
        $item[TOUR_CURRENCY] = createSelectTagFormData( TOUR_CURRENCY, "full-width", $GLOBALS['currency'], $item[TOUR_CURRENCY]);
        $item[TOUR_SHOWCURRENCY] = createSelectTagFormData( TOUR_SHOWCURRENCY, "full-width", $GLOBALS['currency'], $item[TOUR_SHOWCURRENCY]);
        
        $carCtrl = new CategoryController( "");
        $cateArray = $carCtrl->adminCateArray( $item[TOUR_TYPE]);
        $item[TOUR_CATEGORYID] = createSelectTagFormData( TOUR_CATEGORYID, "full-width", $cateArray, $item[TOUR_CATEGORYID]);
        
        // assign gird title
        $template->assign( FORMTITLE, $title);
        
        // assign form item
        $template->assign( ITEM, $item);
        $template->parse( PAGECONTENT . "." . ITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }
    
    /**
     * 
     * @return multitype:string
     */
    public function submitForm(){
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        
        $item[TOUR_ID] = $_REQUEST[CATEGORY_ID];
		$item[TOUR_TITLE] = $_REQUEST[TOUR_TITLE];
		
		$item[TOUR_LOCATION] = $_REQUEST[TOUR_LOCATION];
		$item[TOUR_DESTINATION] = $_REQUEST[TOUR_DESTINATION];
		$item[TOUR_TRANSPORT] = $_REQUEST[TOUR_TRANSPORT];
		$item[TOUR_DURATION] = $_REQUEST[TOUR_DURATION];
		$item[TOUR_CALENDAR] = $_REQUEST[TOUR_CALENDAR];
		
		$item[TOUR_IMAGELINK] = $_REQUEST[TOUR_IMAGELINK];
		
		$item[TOUR_CATEGORYID] = $_REQUEST[TOUR_CATEGORYID];
		$item[TOUR_TYPE] = $_REQUEST[TOUR_TYPE];
		
		$item[TOUR_PRICE] = editEntryPrice( $_REQUEST[TOUR_PRICE]);
		$item[TOUR_CURRENCY] = $_REQUEST[TOUR_CURRENCY];
		$item[TOUR_SHOWPRICE] = editEntryPrice( $_REQUEST[TOUR_SHOWPRICE]);
		$item[TOUR_SHOWCURRENCY] = $_REQUEST[TOUR_SHOWCURRENCY];
		
		$item[TOUR_DISCOUNT] = $_REQUEST[TOUR_DISCOUNT];
		$item[TOUR_PROMOTION] = $_REQUEST[TOUR_PROMOTION];
		
		$item[TOUR_DESCRIPTION] = $_REQUEST[TOUR_DESCRIPTION];
		$item[TOUR_CONTENT] = $_REQUEST[TOUR_CONTENT];
		$item[TOUR_SCHEDULE] = $_REQUEST[TOUR_SCHEDULE];
		
		$item[TOUR_HOT] = $_REQUEST[TOUR_HOT];
		$item[TOUR_FESH] = $_REQUEST[TOUR_HOT];
		
        $item[TOUR_TITLETAG] = $_REQUEST[ARTICLE_TITLETAG];
        $item[TOUR_URL] = encodeURL( $_REQUEST[ARTICLE_URL]);
        $item[TOUR_METAKEYWORD] = $_REQUEST[ARTICLE_METAKEYWORD];
        $item[TOUR_METADESCRIPTION] = $_REQUEST[ARTICLE_METADESCRIPTION];
        
        $item[TOUR_UPDATETIME] = getCurrentDateTime();
        $item[TOUR_LANGUAGE] = $this->lan;
        $item[TOUR_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        if (empty($item[TOUR_TITLETAG])){
            $item[TOUR_TITLETAG] = $item[TOUR_TITLE];
        }
        if (empty($item[ARTICLE_URL])){
            $item[TOUR_URL] = encodeURL( $_REQUEST[TOUR_TITLE]);
        }
        
        // check url
        if (checkURLExist( TOURS, $item[TOUR_URL], $item[TOUR_ID])) {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = getMessage( MESSAGE_URL_EXISTED);
        
            // return repnose array;
            return $response_array;
        }
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[TOUR_ID] == 0) {
            if ($db->insertData( $item, TOURS)) {
                $item = $db->selectOneByCause( array(TOUR_ID, TOUR_URL), TOURS, array(TOUR_URL => $item[TOUR_URL]));
                updateDataType(TOURS, $item[TOUR_ID], $item[TOUR_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_TOURS . "&act=" . PVALUE_ACTION_VIEW_TABLE . "&type=" . $_REQUEST[TOUR_TYPE];
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, TOURS)) {
                updateDataType(TOURS, $item[TOUR_ID], $item[TOUR_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_TOURS . "&act=" . PVALUE_ACTION_VIEW_TABLE . "&type=" . $_REQUEST[TOUR_TYPE];
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        
        
        // return repnose array;
        return $response_array;
        
    }
    
    /**
     * submit delete one function
     * @return multitype:
     */
    public function submitDeleteOne(){
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
        
            $db = new DBAccess();
        
            if ($db->deleteData( TOURS, $_REQUEST[PARAM_ID])) {
                deleteDataType(TOURS, $_REQUEST[PARAM_ID]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
            }
        } else {
            $response_array[STATUS] = STATUS_FAIL;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }
    
    /**
     * submit delete many function
     * @return multitype:
     */
    public function submitDeleteMany(){
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_SELECTED])) {
        
            $IDArray = $_REQUEST[PARAM_SELECTED];
            
            if ($IDArray != null && count( $IDArray) > 0) {
                $delete = true;
                $db = new DBAccess();
                foreach ( $IDArray as $id ) {
                    if ($db->deleteData( TOURS, $id)) {
                        deleteDataType(TOURS, $id);
                    } else {
                        $delete = false;
                    }
                }
        
                if ($delete) {
                    $response_array[STATUS] = STATUS_OK;
                    $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
                } else {
                    $response_array[STATUS] = STATUS_FAIL;
                    $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
                }
            } else {
                $response_array[STATUS] = STATUS_ERROR;
                $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
            }
        } else {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>